/*
Kakine
ActionScript Framework
Copyright © 2011, PUNY, LLC.

This file is part of KAKINE.

KAKINE is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

KAKINE is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with KAKINE.  If not, see <http://www.gnu.org/licenses/>.
*/ 
package com.puny.ball.views.obstacles
{
	import com.puny.ball.utils.CollisionPL;
	import com.puny.ball.utils.Vector2D;
	
	import flash.events.Event;
	import flash.geom.Rectangle;
	
	public class LineObstacle extends Obstacle
	{
		private const _cpl:CollisionPL = new CollisionPL();
		private const _dir:Vector2D = new Vector2D();
		private const _pos:Vector2D = new Vector2D();
		public function LineObstacle()
		{
			super();
		}
		
		override protected function addedStageHandler(event:Event):void {
			super.addedStageHandler(event);
//			gotoAndPlay(Random.integer(totalFrames-1)+1);
		}
		
		override protected function drawShape():void {
			graphics.clear();
			graphics.lineStyle(3,0xFFFF00);
			graphics.moveTo(_cpl.r.x,_cpl.r.y);
			graphics.lineTo(_cpl.r.x + _cpl.dir.x,_cpl.dir.y + _cpl.dir.y);
		}
		
		public function inflate(r:Number):void {
			var rect:Rectangle = getBounds(this);
			_cpl.r.x = x;
			_cpl.r.y = y;
			_dir.x = rect.width;
			_dir.y = 1;//rect.height;
			_cpl.dir = _dir;
		}
		override public function collision(org:Vector2D, dir:Vector2D):Boolean {
			if (currentLabel == "open") {
				return false;
			}
			pos = Infinity;
			var t:Number;
			trace("LineObstacle=",_cpl.r, _cpl.dir);
			if (_cpl.setParticle(org,dir)) {
				t = _cpl.collisionPoint();
				if (t < pos) {
					pos = t;
					newPos = _cpl.reflex();
					mirroredPos = _cpl.mirror();
				}
			}
			if (pos != Infinity) {
				_pos.x = org.x + pos * dir.x;
				_pos.y = org.y + pos * dir.y;
				hitPos = _pos;
			}
			return pos != Infinity;
		}
	}
}